
export default {
  openNewLink(src, data = {}) {
    let $a = document.createElement('form');
    $a.setAttribute('action', src);
    $a.setAttribute('target', '_blank');
    $a.setAttribute('download', '');
    Object.keys(data).forEach(key => {
      let input = document.createElement('input');
      input.setAttribute('type', 'hidden');
      input.setAttribute('name', key);
      let v;
      if (typeof data[key] === 'string' || typeof data[key] === 'number') {
        v = data[key];
      } else {
        v = JSON.stringify(data[key]);
      }
      input.setAttribute('value', v);
      $a.appendChild(input);
    });

    let body = document.getElementsByTagName('body')[0];
    body.appendChild($a);
    $a.submit();
    body.removeChild($a);
  },
  uploadUrl: process.env.BASE_API + '/file/upload',
  downloadUrl: process.env.BASE_API + '/file/download',
  checkCode(string) {
    if (string.length > 32) {
      return '内容过长了';
    }
    let reg = /^[a-zA-Z0-9\-_]{1,32}$/ig;
    if (reg.test(string)) {
      return true;
    } else {
      return '仅可用英文字母数字及_-组成';
    }
  },
  validKey(string) {
    if (string.length > 64) {
      return '内容过长了';
    }
    let reg = /^[a-zA-Z0-9\-_\.]{1,64}$/ig;
    if (reg.test(string)) {
      return true;
    } else {
      return '仅可用英文字母数字及_-.组成';
    }
  },
  validName(rule, value, callback) {
    if (value === '') {
      callback(new Error('请输入'));
    } else {
      if (value.length > 40) {
        callback(new Error('内容过长了'));
      }
      let reg = /^[\u4e00-\u9fa5_\-a-zA-Z0-9\.]{1,40}$/ig;
      if (!reg.test(value)) {
        callback(new Error('仅可用英文字母数字及_-组成及中文组成'));
      } else {
        callback();
      }
    }
  },

  hasClass: function (elem, cls) {
    cls = cls || '';
    if (cls.replace(/\s/g, '').length === 0) return false;
    return new RegExp(' ' + cls + ' ').test(' ' + elem.className + ' ');
  },
  addClass: function (elem, cls) {
    if (!this.hasClass(elem, cls)) {
      elem.className = elem.className === '' ? cls : elem.className + ' ' + cls;
    }
  },
  removeClass: function (elem, cls) {
    if (this.hasClass(elem, cls)) {
      let newClass = ' ' + elem.className.replace(/[\t\r\n]/g, '') + ' ';
      while (newClass.indexOf(' ' + cls + ' ') >= 0) {
        newClass = newClass.replace(' ' + cls + ' ', ' ');
      }
      elem.className = newClass.replace(/^\s+|\s+$/g, '');
    }
  },
  pushHistory: function (name, url) {
    if (url) {
      url = window.location.pathname + '#' + url;
    }
    history.pushState({}, name, url);
  },
  deepClone: function (obj) {
    let proto = Object.getPrototypeOf(obj);
    return Object.assign({}, Object.create(proto), obj);
  },
  isString(val) {
    return typeof val === 'string';
  },
  trim(str) {
    return str.replace(/^\s*/, '').replace(/\s*$/, '');
  },
  getFontThumbUrl(nameStr = '', thumbWidth = 60, thumbHeight = 60) {
    let fillString, ascCode, colorIndex, canvas, canvas2d, url, fillColorArr;
    fillColorArr = ['#1abc9c', '#2ecc71', '#3498db', '#9b59b6', '#34495e', '#16a085', '#27ae60', '#2980b9', '#8e44ad', '#2c3e50', '#f1c40f', '#e67e22', '#e74c3c', '#f39c12', '#d35400', '#c0392b', '#bdc3c7', '#7f8c8d'];
    fillString = String(nameStr).toUpperCase().substr(-4);
    if (window.devicePixelRatio) {
      thumbWidth *= window.devicePixelRatio;
      thumbHeight *= window.devicePixelRatio;
    }
    ascCode = (fillString.substr(-1) === '?' ? 72 : fillString.substr(-1).charCodeAt(0)) - 64;
    colorIndex = Math.abs(ascCode % 20);
    canvas = document.createElement('canvas');
    canvas.width = thumbWidth;
    canvas.height = thumbHeight;
    canvas2d = canvas.getContext('2d');
    canvas2d.fillStyle = fillColorArr[colorIndex - 1];
    canvas2d.fillRect(0, 0, canvas.width, canvas.height);
    canvas2d.font = Math.round(canvas.width / 8) + 'px Arial';
    canvas2d.textAlign = 'center';
    canvas2d.fillStyle = '#FFF';
    canvas2d.fillText(fillString, thumbWidth / 2, thumbHeight / 1.8);
    url = canvas.toDataURL();
    canvas = null;
    return url;
  },

  gradient(startColor, endColor, step) {
    let rgbToHex = function (r, g, b) {
      let hex = ((r << 16) | (g << 8) | b).toString(16);
      return '#' + new Array(Math.abs(hex.length - 7)).join('0') + hex;
    };
    let hexToRgb = function (hex) {
      let rgb = [];
      for (let i = 1; i < 7; i += 2) {
        rgb.push(parseInt('0x' + hex.slice(i, i + 2), 16));
      }
      return rgb;
    };
    let sColor = hexToRgb(startColor);
    let eColor = hexToRgb(endColor);

    let rStep = (eColor[0] - sColor[0]) / step;
    let gStep = (eColor[1] - sColor[1]) / step;
    let bStep = (eColor[2] - sColor[2]) / step;
    let gradientColorArr = [];
    for (let i = 0; i < step; i++) {
      gradientColorArr.push(rgbToHex(parseInt(rStep * i + sColor[0], 10), parseInt(gStep * i + sColor[1], 10), parseInt(bStep * i + sColor[2], 10)));
    }
    return gradientColorArr;
  }
};
